package com.neusoft.nepm.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.common.domain.*;
import com.neusoft.nepm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/administrator")
public class AdministratorController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private CityService cityService;

    @Autowired
    private AQIlevelService aqIlevelService;
    @Autowired
    private NepgServiceClient nepgServiceClient;
    @Autowired
    private NepsServiceClient nepsServiceClient;

    @PostMapping("/getMesgList")
    public FyResponse getList(@RequestBody FyMesg fyMesg){
        Map<String, Object> conditionMap = new HashMap<>();
        List<Map<String,Object>> list=new ArrayList<>();
        if (fyMesg.getMessage().get("province")!=null){
        conditionMap.put("province",fyMesg.getMessage().get("province"));}
        if (fyMesg.getMessage().get("city")!=null){
        conditionMap.put("city",fyMesg.getMessage().get("city"));}
        if (fyMesg.getMessage().get("level")!=null){
        conditionMap.put("level",fyMesg.getMessage().get("level"));}
        if (fyMesg.getMessage().get("date")!=null){
        conditionMap.put("date",fyMesg.getMessage().get("date"));}
        if (fyMesg.getMessage().get("status")!=null){
        conditionMap.put("status",fyMesg.getMessage().get("status"));}
        Page<SupervisorMesg> page=new Page<>(fyMesg.getPageNum(), fyMesg.getPageSize());
        SupervisorMesgRequest request = new SupervisorMesgRequest();
        request.setConditionMap(conditionMap);
        request.setPage(page);
        IPage<SupervisorMesg> mes= nepsServiceClient.returnList(request);
        for(SupervisorMesg mesg:mes.getRecords()){
            Map<String,Object> result = new HashMap<>();
            result.put("id",mesg.getId());
            result.put("name",nepsServiceClient.getById(mesg.getSupervisor_phone()).getName());
            result.put("province",cityService.find(mesg.getProvince()));
            result.put("city",cityService.findCity(mesg.getCity()));
            result.put("date",mesg.getDate());
            result.put("time",mesg.getTime());
            result.put("status",mesg.getStatus());
            switch (mesg.getLevel()){
                case 1:
                    result.put("level","优（一）");
                    break;
                case 2:
                    result.put("level","良（二）");
                    break;
                case 3:
                    result.put("level","轻度污染（三）");
                    break;
                case 4:
                    result.put("level","中度污染（四）");
                    break;
                case 5:
                    result.put("level","重度污染（五）");
                    break;
                case 6:
                    result.put("level","严重污染（六）");
                    break;
            }
            list.add(result);
        }
        FyResponse response = new FyResponse();
        response.setResult(list);
        response.setTotal((int)mes.getTotal());
        return response;
    }
    //根据信息id获取信息完整属性
    // TODO: 2024/6/24 分页失效 
    @GetMapping("/getchoose")
    public Map<String,Object> getchoose(int id){
        Map<String,Object> map = new HashMap<>();
        SupervisorMesg get = nepsServiceClient.findOne(id).get(0);
        Supervisor supervisor = nepsServiceClient.getById(get.getSupervisor_phone());
        if (get.getGrider_id()!=null){
        Grider grider = nepgServiceClient.findGrider(get.getGrider_id());
        map.put("Gname",grider.getName());
        map.put("Gphone",grider.getTel());
        }
        System.out.println(get);
        System.out.println(supervisor);
        map.put("id",id);
        map.put("Sname",supervisor.getName());
        if (supervisor.getSex()==0){
            map.put("sex","女");
        }else {
            map.put("sex","男");
        }
        map.put("Sphone",supervisor.getPhoneNumber());
        map.put("province",cityService.find(get.getProvince()));
        map.put("city",cityService.findCity(get.getCity()));
        map.put("address",get.getAddress());
        map.put("message",get.getMessage());
        map.put("level",get.getLevel());
        map.put("date",get.getDate());
        map.put("time",get.getTime());
        if(nepgServiceClient.findTest(get.getId())!=null) {
            map.put("Cdate", nepgServiceClient.findTest(get.getId()).getDate());
            map.put("Ctime", nepgServiceClient.findTest(get.getId()).getTime());
        }
        switch (get.getLevel()){
            case 1:
                map.put("level","优（一）");
                break;
            case 2:
                map.put("level","良（二）");
                break;
            case 3:
                map.put("level","轻度污染（三）");
                break;
            case 4:
                map.put("level","中度污染（四）");
                break;
            case 5:
                map.put("level","重度污染（五）");
                break;
            case 6:
                map.put("level","严重污染（六）");
                break;
        }
        //用于确定是否有指派选项
        map.put("status",get.getStatus());
        map.put("birthday",supervisor.getBirthday());
        return map;
    }
    @GetMapping("/getProvince")
    public List<Province> getProvinceList(){
        return cityService.getList();
    }
    @GetMapping("/getProvinceExcept")
    public List<Province> getProvinceEx(int province){
        if (cityService.query(province).size()==1){
            return cityService.getExceptProvince(province);
        }else return cityService.getList();
    }
    @GetMapping("/getCityList")
    public List<City> getCityList(@RequestParam(required = false) Integer id){
        if (id==null){
            return cityService.listAll();
        }else {return cityService.query(id);}

    }
    @GetMapping("/getLevelList")
    public List<AQIlevel> getLevelList(){
       return aqIlevelService.getList();
    }
    //排除非异地指派的城市
    @GetMapping("/getCity")
    public List<City> getCity(Integer id,Integer cid){
        System.out.println(id);
        System.out.println(cid);

        return  cityService.queryNot(id,cid);
    }
    //查询指定城市的网格员
    @GetMapping("/getGriders")
    public  List<Grider> getGriders(int id,int cid){
        return nepgServiceClient.queryList(id,cid);
    }
    //更新公众监督信息的所属网格员以及status
    @GetMapping("/updateMesg")
    public Boolean update(int bindID,String code){
        return nepsServiceClient.updateGrider(bindID,code);
    }
    //查询已经被网格员确定过的信息列表（status=3）
    @PostMapping("/getFinished")
    public FyResponse getFinish(@RequestBody FyMesg fyMesg){
        List<Map<String,Object>> map = new ArrayList<>();
        Map<String, Object> conditionMap = new HashMap<>();
        if (fyMesg.getMessage().get("province")!=null){
            conditionMap.put("province",fyMesg.getMessage().get("province"));}
        if (fyMesg.getMessage().get("city")!=null){
            conditionMap.put("city",fyMesg.getMessage().get("city"));}
        if (fyMesg.getMessage().get("date")!=null){
            conditionMap.put("date",fyMesg.getMessage().get("date"));}
            conditionMap.put("status",3);
        //拿到列表
        Page<SupervisorMesg> page=new Page<>(fyMesg.getPageNum(), fyMesg.getPageSize());
        SupervisorMesgRequest request = new SupervisorMesgRequest();
        request.setConditionMap(conditionMap);
        request.setPage(page);
        IPage<SupervisorMesg> mes= nepsServiceClient.returnList(request);
        for (SupervisorMesg mesg: mes.getRecords()){
            Map<String,Object> result = new HashMap<>();
            result.put("id",mesg.getId());
            result.put("SupervisorName",nepsServiceClient.getById(mesg.getSupervisor_phone()).getName());
            result.put("province",cityService.find(mesg.getProvince()).getName());
            result.put("city",cityService.findCity(mesg.getCity()).getName());
            result.put("date",nepgServiceClient.findTest(mesg.getId()).getDate());
            result.put("time",nepgServiceClient.findTest(mesg.getId()).getTime());
            result.put("GriderName",nepgServiceClient.findGrider(mesg.getGrider_id()).getName());
            switch (nepgServiceClient.findTest(mesg.getId()).getLevel()){
                case 1:
                    result.put("level","优（一）");
                    break;
                case 2:
                    result.put("level","良（二）");
                    break;
                case 3:
                    result.put("level","轻度污染（三）");
                    break;
                case 4:
                    result.put("level","中度污染（四）");
                    break;
                case 5:
                    result.put("level","重度污染（五）");
                    break;
                case 6:
                    result.put("level","严重污染（六）");
                    break;
            }
            map.add(result);
        }
        FyResponse response = new FyResponse();
        response.setResult(map);
        response.setTotal((int)mes.getTotal());
        return response;
    }

    @GetMapping("/getProvinceMesg")
    public List<Map<String,Object>> getProvinceMesg(){
        String sql = "SELECT * FROM province_data";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        return result;
    }

    @GetMapping("/getlevelCount")
    public List<Map<String,Object>> getLevelCount(){
        String sql = "SELECT * FROM level_count";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        return result;
    }

    @GetMapping("/getMonthCount")
    public List<Map<String,Object>> getMonthCount(){
        String sql = "SELECT * FROM month_level_count";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        return result;
    }

    @GetMapping("/getOtherMesg")
    public Map<String,Object> getOthers(){
        Map<String,Object> result =new HashMap<>();
        result.put("test_total",nepgServiceClient.countMesg());
        result.put("well_total",nepgServiceClient.countWell());
        result.put("capital_cover",(double)nepgServiceClient.countCapital()/34*100);
        result.put("big_city_cover",(double)nepgServiceClient.countBigCity()/619*100);
        return result;
    }
    //外部服务提供的调用方法
    @GetMapping("/find")
    public Province find(int id){
        return cityService.find(id);
    }
    @GetMapping("/get-province")
    public List<Province> getAllProvince(){return cityService.getList();}
    @GetMapping("/query")
    public List<City> query(int id){
        return  cityService.query(id);
    }
    @GetMapping("/find-city")
    public City findCity(int id){
        return  cityService.findCity(id);
    }
    @GetMapping("/get-list")
    public List<AQIlevel> getList(){
        return aqIlevelService.getList();
    }
    @GetMapping("/get-tip")
    public String getTip(int id){
        return aqIlevelService.getTip(id);
    }
    @GetMapping("/get-msg")
    public String getMsg(int id){
        return aqIlevelService.getMsg(id);
    }
    @GetMapping("/SO2-test")
    public List<AQIlevel> SO2Test(float SO2){
        return aqIlevelService.SO2Test(SO2);
    }
    @GetMapping("/CO-test")
    public List<AQIlevel> COTest(float CO){
        return aqIlevelService.COTest(CO);
    }
    @GetMapping("/PM-test")
    public List<AQIlevel> PMTest(float PM){
        return aqIlevelService.PMTest(PM);
    }
}
